﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using CommonControlLibrary.Common;
using DBAccessLibrary.Model;
using QueryResource.Common;
using QueryResource.ViewModel;

namespace DevControlLibrary.UI
{
    /// <summary>
    /// DBTreeViewControl.xaml 的交互逻辑
    /// </summary>
    [System.ComponentModel.DesignTimeVisible(false)]
    public partial class DBTreeViewControl 
    {
        public DBTreeViewControl()
        {
            
                InitializeComponent();
                ViewModel = new DBTreeViewModel();
           
        }
        public DBTreeViewModel ViewModel { get; set; }
        public void Init()
        {

            ViewModel.TreeViewBegionInvoke = fun => this.dbtreeview.BeginInvokeEx(fun);
            ViewModel.LoadData();
            ViewModel.ShowDescriptionWindows = entity =>
                                                   {
                                                       AddDescriptionWindow window = new AddDescriptionWindow(entity);
                                                       window.ShowDialog();
                                                   };
            this.dbtreeview.DataContext = ViewModel;

        }

        /// <summary>
        /// 右键 单击 默认选择节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DbtreeviewPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem item = GetTemplatedAncestor<TreeViewItem>(e.OriginalSource as FrameworkElement);
            if (item != null)
            {
                item.IsSelected = true;
            }
        }
        private T GetTemplatedAncestor<T>(FrameworkElement element) where T : FrameworkElement
        {
            if (element is T)
            {
                return element as T;
            }

            FrameworkElement templatedParent = element.TemplatedParent as FrameworkElement;
            if (templatedParent != null)
            {
                return GetTemplatedAncestor<T>(templatedParent);
            }

            return null;
        }

        /// <summary>
        /// 选择节点 触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DbtreeviewSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            dbtreeview.Focus();
            ViewModel.SelectedItem = dbtreeview.SelectedItem as DataBaseEntity;
        }
    }


    public class ContextMenuDataTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            FrameworkElement element = container as FrameworkElement;
            DataTemplate template = null;
            if (element == null)
                return null;
            if (item is ServerEntity)
            {
                template = element.FindResource("ServerTemplate") as HierarchicalDataTemplate;
            }
            else if (item is DBEntity)
            {
                template = element.FindResource("DBTemplate") as HierarchicalDataTemplate;
            }
            else if (item is TableInfoEntity)
            {
                template = element.FindResource("TableInfoTemplate") as HierarchicalDataTemplate;
            }
             else if (item is IndexDescriptionEntity)
            {
                template = element.FindResource("IndexDescriptionTemplate") as HierarchicalDataTemplate;
            }
            else if (item is DescriptionEntity)
            {
                template = element.FindResource("DescriptionTemplate") as HierarchicalDataTemplate;
            }
            else if (item is TableEntity)
            {
                template = element.FindResource("TableTemplate") as HierarchicalDataTemplate;
            }
            else if (item is ProcParamEntity)
            {
                template = element.FindResource("ProcParamTemplate") as HierarchicalDataTemplate;
            }
            else if (item is FieldEntity)
            {
                template = element.FindResource("FieldTemplate") as HierarchicalDataTemplate;
            }
            else if (item is ProcEntity)
            {
                template = element.FindResource("ProcTemplate") as HierarchicalDataTemplate;
            }
            else if (item is ViewEntity)
            {
                template = element.FindResource("ViewTemplate") as HierarchicalDataTemplate;
            }
            else if (item is RelationshipEntity)
            {
                template = element.FindResource("ForeignTemplate") as HierarchicalDataTemplate;
            }
            else if (item is IndexEntity)
            {
                template = element.FindResource("IndexTemplate") as HierarchicalDataTemplate;
            }

            return template;
        }
    }

    [ValueConversion(typeof(bool),typeof(string))]
    public class IsNullConverter : IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool isNull = (bool) value;
            return isNull ? "Null" : "Not Null";
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string NullSrt = value.ToString();
            if (NullSrt == "Null")
                return true;
            return false;
        }
    }
}
